package com.sx.jys.ui.frag

import android.os.Bundle
import android.view.ViewGroup
import com.gyf.immersionbar.ImmersionBar
import com.sos.widget.view.flyco.tablayout.listener.OnTabSelectListener
import com.sos.widget.view.refresh.finishLoad
import com.sx.jys.app.framework.BaseFragment
import com.sx.jys.databinding.FragHomeBinding
import com.sx.jys.ui.adapter.HomeListAdapter
import com.sx.jys.ui.vm.HomeVm

class FragHome : BaseFragment<HomeVm, FragHomeBinding>() {
    companion object {
        val instance = FragHome()
    }

    //1-今日 2-本月 3-累计
    var currentType = 1
    override fun initView(savedInstanceState: Bundle?) {
        (mDatabind.tvTitle.layoutParams as ViewGroup.MarginLayoutParams).let {
            it.topMargin = ImmersionBar.getStatusBarHeight(this)
        }

        mDatabind.tab.setTabData(arrayOf("今日", "本月", "累计"))
        mDatabind.tab.setOnTabSelectListener(object : OnTabSelectListener {
            override fun onTabSelect(position: Int) {
                selectType(position+1)
            }

            override fun onTabReselect(position: Int) {
                TODO("Not yet implemented")
            }
        })
        mDatabind.myRefresh.setOnRefreshListener {
            it.finishRefresh(2500)
            mViewModel.reqHomeList(currentType)
        }
    }

    override fun initData() {
        super.initData()
        mDatabind.vm = mViewModel
        mDatabind.adapter = HomeListAdapter(requireContext())
    }


    override fun createObserver() {
        super.createObserver()
        mViewModel.uiState.refreshEvent.observe(this) {
            mDatabind.myRefresh.finishRefresh()
        }
    }

    fun selectType(type: Int){
        currentType = type
        mViewModel.reqHomeList(currentType)
        val strList=arrayOf("当天","本月","累计")
        var typeStr=strList[type-1]
        mDatabind.tvYh.setText("${typeStr}盈亏")
        mDatabind.tvOrder.setText("${typeStr}订单")
        mDatabind.tvRate.setText("${typeStr}胜率")
    }

    override fun onResume() {
        super.onResume()
        mViewModel.reqHomeList(currentType)
    }
}